
#include <BOpenGL>
#include <BShader>
#include "member_BShader.h"

using namespace BWE;

#define member					(*(member_BShader*)_ptr)
#define member_allocate()		_ptr = new member_BShader(this)
#define member_release()		delete (member_BShader*)_ptr

extern BArray<GLuint>	UnusedShaderObjects;

BShader::BShader()
{
	member_allocate();
}
BShader::BShader(Type type, const BString& text)
{
	member_allocate();
	member.type = type;
	member.text = text;
	member.dirty = true;
}
BShader::~BShader()
{
	if (member.object)
	{
		UnusedShaderObjects.append(member.object);
		member.object = 0;
	}
	member_release();
}

void BShader::copy(const BShader* other)
{
	member.type = shader_member(other)->type;
	member.text = shader_member(other)->text;
	member.dirty = shader_member(other)->dirty;
	member.object = shader_member(other)->object;
}
BShader* BShader::clone() const
{
	BShader* shader = new BShader();
	shader->copy(this);
	return shader;
}

void BShader::setType(Type type)
{
	if (member.type != type)
	{
		member.type = type;
		member.dirty = true;
		emit(Signal_Dirty);
	}
}
BShader::Type BShader::type() const
{
	return member.type;
}

void BShader::setText(const BString& text)
{
	if (member.text != text)
	{
		member.text = text;
		member.dirty = true;
		emit(Signal_Dirty);
	}
}
const BString& BShader::text() const
{
	return member.text;
}

void BShader::clear()
{
	if (member.text.size())
	{
		if (member.object)
		{
			UnusedShaderObjects.append(member.object);
			member.object = 0;
		}
		member.object = 0;
		member.text.clear();
		member.dirty = true;
		emit(Signal_Dirty);
	}
}

